import Vue from 'vue'
import VueRouter from "vue-router";
import LoginBasic from "@/pages/LoginBasic.vue";
import BackedBasic from "@/components/BackedBasic.vue";
import HomeBasic from "@/pages/HomeBasic.vue";
import InfoBasic from "@/pages/InfoBasic.vue";
import ImgBasic from "@/pages/ImgBasic.vue";

Vue.use(VueRouter) //使用VueRouter插件

const router=new VueRouter({
    routes:[
        {
            path:"/",
            redirect:'/home'
        },
        {
            path:'/login',
            component:LoginBasic
        },
        {
            path:'/home',
            component:HomeBasic,
            children:[
                {
                    path:'',
                    component:InfoBasic
                },
                {
                    path:'imgUpdate',
                    component:ImgBasic
                }
            ]
        }
    ]
})

router.beforeEach((to,from,next)=>{
    const isLoggedIn=localStorage.getItem("isLoggedIn")
    if (!isLoggedIn&&to.path!=='/login'){
        console.log('触发前置导航守卫')
        next("/login")
    }else{
            next()
    }
})

export default router
